﻿Imports Mysoft.Map.Data
Imports System.Data

Partial Public Class Cwjk_SelectFundParam
    Inherits Mysoft.Map.Application.Security.AppPage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Data_Bind()
            '设置级数
            Dim strLevelHtml As String = ""
            Dim i As Int32
            For i = 1 To 2
                strLevelHtml = strLevelHtml & "<a href='javascript:showLevel(" & i + 1 & ")' style='color:white;text-decoration: none' >&nbsp;" & i & "&nbsp;</a>"
            Next
            sHtml.Value = strLevelHtml
        End If
    End Sub
    Private Sub Data_Bind()
        Dim dtTemp, dtFeeItem As DataTable
        Dim strSQL, strIsUsedScopeRepeat, strParentCode, strCwztGUID, strItemType, strItemName, strOperTypeGUID, strUsedScope, strUsedScopeList As String
        Dim i As Integer = 0
        Dim j As Integer = 0
        Dim k As Integer = 0
        strIsUsedScopeRepeat = Request.QueryString("IsUsedScopeRepeat")
        strParentCode = Request.QueryString("ParentCode")
        strCwztGUID = Request.QueryString("CwztGUID")
        strOperTypeGUID = Request.QueryString("OperTypeGUID")
        If Not Request.QueryString("SelectUsedScope") Is Nothing AndAlso Request.QueryString("SelectUsedScope") <> "" Then
            Me.txtSelectUsedScope.Text = ConertItemTypeToItemName(Request.QueryString("SelectUsedScope"))
        End If

        If strOperTypeGUID Is Nothing Or strOperTypeGUID = "" Then
            strOperTypeGUID = "00000000-0000-0000-0000-000000000000"
        End If

        If strIsUsedScopeRepeat = "True" Then
            strSQL = "SELECT  a.ParamValue,a.ParamCode,(b.ParamValue+'-'+a.ParamValue) AS ItemFullName,b.ParamValue AS ParentName,a.IfEnd,a.ParamGUID " & _
                     " FROM " & _
                     " ( SELECT  ParamValue,ParamCode,ParentCode,IfEnd,ParamGUID  FROM myBizParamOption WHERE paramname = 'cb_FundParam' AND ScopeGUID='" & Session("BUGUID") & "') a " & _
                     " Left Join " & _
                     " ( SELECT  ParamValue,ParamCode,ParentCode,IfEnd,ParamGUID  FROM myBizParamOption WHERE paramname = 'cb_FundParam' AND ScopeGUID='" & Session("BUGUID") & "') b " & _
                     "	ON  b.ParamCode = a.ParentCode ORDER BY a.ParamCode "
        Else
            dtFeeItem = MyDB.GetDataTable("SELECT UsedScope FROM p_CwjkOperType WHERE IsDisable=0 AND CwztGUID = '" & strCwztGUID & "' AND HierarchyCode  LIKE '" & strParentCode & ".%' AND OperTypeGUID <> '" & strOperTypeGUID & "'")
            If dtFeeItem.Rows.Count > 0 Then
                strUsedScope = ""
                strItemType = ""
                strItemName = ""
                strUsedScopeList = ""
                For i = 0 To dtFeeItem.Rows.Count - 1
                    strUsedScope = dtFeeItem.Rows(i).Item("UsedScope").ToString()
                    If strUsedScope <> "" Then
                        strUsedScopeList &= ConertItemTypeToItemName(strUsedScope).Replace("，", ",") & ","
                    End If
                Next

                strUsedScopeList = strUsedScopeList.TrimEnd(",").Replace(",", "','")
                If strUsedScopeList = "" Then
                    strSQL = "SELECT  a.ParamValue,a.ParamCode,(b.ParamValue+'-'+a.ParamValue) AS ItemFullName,b.ParamValue AS ParentName,a.IfEnd,a.ParamGUID " & _
                             " FROM " & _
                             " ( SELECT  ParamValue,ParamCode,ParentCode,IfEnd,ParamGUID  FROM myBizParamOption WHERE paramname = 'cb_FundParam' AND ScopeGUID='" & Session("BUGUID") & "') a " & _
                             " Left Join " & _
                             " ( SELECT  ParamValue,ParamCode,ParentCode,IfEnd,ParamGUID  FROM myBizParamOption WHERE paramname = 'cb_FundParam' AND ScopeGUID='" & Session("BUGUID") & "') b " & _
                             "	ON  b.ParamCode = a.ParentCode ORDER BY a.ParamCode "
                Else
                    strUsedScopeList = "'" & strUsedScopeList & "'"
                    strSQL = "SELECT  DISTINCT parent.ParamValue ,parent.ParamCode, parent.ItemFullName,parent.ParentName,parent.IfEnd,parent.ParamGUID FROM " & _
                             "(" & _
                             "   SELECT  a.ParamValue,a.ParamCode,(b.ParamValue+'-'+a.ParamValue) AS ItemFullName,b.ParamValue AS ParentName, a.IfEnd,a.ParamGUID " & _
                             "   FROM " & _
                             "     ( SELECT  ParamValue,ParamCode,ParentCode,IfEnd,ParamGUID  FROM myBizParamOption WHERE paramname = 'cb_FundParam' AND ScopeGUID='" & Session("BUGUID") & "') a " & _
                             "     Left Join " & _
                             "     ( SELECT  ParamValue,ParamCode,ParentCode,IfEnd,ParamGUID  FROM myBizParamOption WHERE paramname = 'cb_FundParam' AND ScopeGUID='" & Session("BUGUID") & "') b " & _
                             "       ON  b.ParamCode = a.ParentCode  " & _
                             ") Parent " & _
                             "  INNER JOIN " & _
                             "( " & _
                             "   SELECT  a.ParamValue,a.ParamCode,(b.ParamValue+'-'+a.ParamValue) AS ItemFullName,b.ParamValue AS ParentName " & _
                             "   FROM " & _
                             "      (  SELECT  ParamValue,ParamCode,ParentCode,IfEnd,ParamGUID  FROM myBizParamOption WHERE paramname = 'cb_FundParam' AND ScopeGUID='" & Session("BUGUID") & "') a " & _
                             "      Left Join " & _
                             "      (  SELECT  ParamValue,ParamCode,ParentCode,IfEnd,ParamGUID  FROM myBizParamOption WHERE paramname = 'cb_FundParam' AND ScopeGUID='" & Session("BUGUID") & "') b " & _
                             "      ON  b.ParamCode = a.ParentCode " & _
                             "   WHERE (b.ParamValue+'-'+a.ParamValue) NOT IN (" & strUsedScopeList & ") and a.IfEnd = 1" & _
                             ") FeeItem " & _
                             " ON FeeItem.ParamCode + '.' like Parent.ParamCode +'.%' " & _
                             " ORDER BY Parent.ParamCode"
                End If
            Else
                strSQL = "SELECT  a.ParamValue,a.ParamCode,(b.ParamValue+'-'+a.ParamValue) AS ItemFullName,b.ParamValue AS ParentName,a.IfEnd,a.ParamGUID " & _
                         " FROM " & _
                         " ( SELECT  ParamValue,ParamCode,ParentCode,IfEnd,ParamGUID  FROM myBizParamOption WHERE paramname = 'cb_FundParam' AND ScopeGUID='" & Session("BUGUID") & "') a " & _
                         " Left Join " & _
                         " ( SELECT  ParamValue,ParamCode,ParentCode,IfEnd,ParamGUID  FROM myBizParamOption WHERE paramname = 'cb_FundParam' AND ScopeGUID='" & Session("BUGUID") & "') b " & _
                         "	ON  b.ParamCode = a.ParentCode ORDER BY a.ParamCode "
            End If
        End If

        dtTemp = MyDB.GetDataTable(strSQL)

        '自定义控件绑定数据
        repItem.BindMode = 1                                          '绑定模式：0 -- SQL 语句绑定，1 -- DataTable 绑定
        repItem.DataTable = dtTemp
        repItem.DataBind()                                            '绑定数据
    End Sub

    '转换适用范围的格式，将“工程合同款(进度款、结算款)，材料设备款(进度款、结算款)”格式转换成“工程合同款-进度款，工程合同款-结算款，材料设备款-进度款，材料设备款-结算款”
    Function ConertItemTypeToItemName(ByVal strUsedScope As String) As String
        Dim strNewUsedScope As String = ""
        Dim i, j As Integer
        If strUsedScope = "" Then
            Return strNewUsedScope
        End If
        Dim arrUsedScope As String() = strUsedScope.Split("，")
        For i = 0 To arrUsedScope.Length - 1
            Dim arrItemType As String() = arrUsedScope(i).TrimEnd(")").Split("(")
            Dim strItemType As String = ""
            If arrItemType.Length = 2 Then
                strItemType = arrItemType(0)
                Dim arrItemName As String() = arrItemType(1).Split("、")
                For j = 0 To arrItemName.Length - 1
                    strNewUsedScope &= strItemType & "-" & arrItemName(j) & "，"
                Next
            End If
        Next
        strNewUsedScope = strNewUsedScope.Trim("，")
        Return strNewUsedScope
    End Function

End Class